package org.file5.service.impl;

import java.io.IOException;
import java.util.Date;

import net.sf.jmimemagic.MagicException;
import net.sf.jmimemagic.MagicMatch;
import net.sf.jmimemagic.MagicMatchNotFoundException;
import net.sf.jmimemagic.MagicParseException;

import org.apache.log4j.Logger;
import org.file5.dao.DaoFascade;
import org.file5.domain.BaseEntity.STATUS;
import org.file5.domain.EntityComment;
import org.file5.domain.Person;
import org.file5.service.PersonService;
import org.file5.util.DataBean;
import org.file5.util.DesEncrypter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.users.User;

@Service("personService")
public class PersonServiceImpl implements PersonService {

	private final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	DaoFascade df;

	@Override
	public Person addUpdate(User googleUser, String blogUrl, String blogJid) {
		String personId = this.getPersonUid(googleUser);
		Person person = df.getBaseDao().find(Person.class, personId);
		if (person == null) {
			person = new Person();
			person.setUid(personId);
		}
		person.setName(googleUser.getNickname());
		person.setEmail(googleUser.getEmail());

		if (blogUrl != null && !blogUrl.isEmpty()) {
			person.setBlogUrl(blogUrl);

		}
		if (blogJid != null && !blogJid.isEmpty()) {
			person.setBlogJid(blogJid);
		}
		df.getBaseDao().addUpdate(person);
		return person;
	}

	public String getPersonUid(User googleUser) {
		String encEmail = null;
		if (googleUser != null) {
			DesEncrypter crypter = new DesEncrypter();
			encEmail = crypter.encrypt(googleUser.getEmail());
		}
		return encEmail;
	}

	@Override
	public Person find(String personUid) {
		Person person = df.getBaseDao().find(Person.class, personUid);
		return person;
	}

	@Override
	public DataBean<Person> getPersons(int startPosition, int maxResult) {
		DataBean<Person> db = df.getBaseDao().getListByProperties(Person.class, startPosition, maxResult, null, null);
		return db;
	}

	@Override
	public Person updateStatus(String uid, STATUS status) {
		Person person = df.getBaseDao().find(Person.class, uid);
		if (person != null) {
			person.setStatus(status);
			person.setStatusUpdated(new Date());
			df.getBaseDao().addUpdate(person);
		}
		return person;
	}

	@Override
	public Person updateProfile(Person person) {
		Person thePerson = df.getBaseDao().find(Person.class, person.getUid());
		MultipartFile mpf = person.getMultipartFile();
		if (mpf != null && mpf.getSize() > 0) {
			try {
				thePerson.setAvatarIcon(new Blob(mpf.getBytes()));
			} catch (IOException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			}
		}
		thePerson.setSignature(person.getSignature());
		thePerson.setLongDesc(person.getLongDesc());
		thePerson = df.getBaseDao().addUpdate(thePerson);
		return thePerson;
	}
}
